<template>
  <div id="page-notice">
    <!-- 查询 -->
    <div class="page-search-list">
      <el-form
        label-position="left"
        :model="search_form"
        :inline="true"
        ref="healthFromRef"
      >
        <el-form-item
          v-for="(item, i) in search_info"
          :label="item.label"
          :key="i"
          style="margin-bottom: 5px;"
        >
          <template v-if="item.prop === 'start_time'">
            <template v-if="!is_time_period">
              <el-date-picker
                type="dates"
                v-model="search_form.time"
                placeholder="选择一个或多个日期"
                key="time"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
            <template v-else>
              <el-date-picker
                v-model="search_form.time_period"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                key="time_period"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
          </template>
          <template v-else-if="item.prop === 'cid'">
            <select-community
              :cid.sync="search_form[item.prop]"
              :pid.sync="search_form.pid"
              :bid.sync="search_form.bid"
              :sid.sync="search_form.sid"
            ></select-community>
          </template>
          <template v-else>
            <el-input
              v-model="search_form[item.prop]"
              size="small"
              :placeholder="`请输入${item.label}`"
            ></el-input>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button class="el-icon-search" size="small" @click.native="search">
            搜索
          </el-button>
          <el-button
            type="primary"
            class="el-icon-refresh"
            size="small"
            @click.native="resetSearchFields"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="page-botton-list">
      <el-button
        plain
        class="el-icon-date"
        size="mini"
        @click.native="is_time_period = !is_time_period"
      >
        {{ is_time_period ? '时间查询' : '时间段查询' }}
      </el-button>
      <el-button
        type="primary"
        plain
        class="el-icon-bell"
        size="mini"
        @click="notice_dialog_visible = true"
        key="add_button"
      >
        新增通知
      </el-button>
      <el-button
        type="danger"
        plain
        class="el-icon-delete"
        size="mini"
        :disabled="is_delete"
        @click="deleteNotice"
        key="delete_arr_button"
      >
        删除
      </el-button>
    </div>
    <!-- 表格 -->
    <div class="page-table">
      <el-table
        :data="notice_table"
        style="width: 100%;"
        id="notice_tableId"
        ref="noticeTableRef"
        @selection-change="selectTabel"
      >
        <el-table-column type="selection" />
        <el-table-column
          v-for="(column, i) in tableInfo"
          align="center"
          :key="i"
          :label="column.label"
          :width="column.prop === 'title' ? '350px' : ''"
        >
          <template slot-scope="scope">
            <template v-if="column.prop === 'title'">
              <el-collapse v-model="activeNames">
                <el-collapse-item :name="i">
                  <div slot="title" style="width: 100%;">
                    {{ scope.row[column.prop] }}
                  </div>
                  <div style="text-indent: 2em; text-align: left;">
                    {{ scope.row.content }}
                  </div>
                </el-collapse-item>
              </el-collapse>
            </template>
            <template v-else>{{ scope.row[column.prop] }}</template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <el-button
              type="danger"
              plain
              size="mini"
              class="el-icon-delete"
              @click="deleteNotice(scope.row.id, scope.row.title)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageCount"
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tatal"
        class="upload-list-pagination"
      ></el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog
      title="发布通知"
      width="40%"
      :visible.sync="notice_dialog_visible"
      :append-to-body="true"
    >
      <el-form
        :model="add_notice_dialog"
        label-position="left"
        :rules="rules"
        ref="addDialogRef"
      >
        <el-form-item
          v-for="(item, i) in dialog_info"
          :key="i"
          :label="item.label"
          label-width="80px"
          :prop="item.prop"
        >
          <template v-if="item.prop === 'cid'">
            <select-community
              :cid.sync="add_notice_dialog[item.prop]"
              :pid.sync="add_notice_dialog.pid"
              :bid.sync="add_notice_dialog.bid"
              :sid.sync="add_notice_dialog.sid"
            ></select-community>
          </template>
          <template v-else-if="item.prop === 'content'">
            <el-input
              type="textarea"
              autosize
              :autosize="{ minRows: 3 }"
              placeholder="请输入内容"
              v-model="add_notice_dialog[item.prop]"
            ></el-input>
          </template>
          <template v-else>
            <el-input
              v-model="add_notice_dialog[item.prop]"
              size="small"
            ></el-input>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="addClick">
          确定添加
        </el-button>
      </div>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="delete_dialog_visible"
      width="30%"
      center
      :append-to-body="true"
    >
      <div style="text-align: center;">
        是否删除以下通知
        <li v-for="(item, i) in delete_user_arr" :key="i">
          {{ i + 1 + '.' + item.title + `（${item.id}）` }}
        </li>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="falsedeleteNotice">取 消</el-button>
        <el-button type="primary" @click="truedeleteNotice">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { rules } from '@/utils/rules'
// 工具类
import { isNull, assignAttr } from '@/utils/tool.js'
export default {
  computed: {
    is_delete() {
      return !this.multiple_selection.length > 0
    },
  },
  watch: {
    search_form: {
      handler(val) {
        console.log(val)
      },
    },
    notice_dialog_visible: {
      handler(val) {
        if (!val) {
          this.add_notice_dialog = {}
          this.$refs.addDialogRef.clearValidate()
        }
      },
    },
    delete_dialog_visible: {
      handler(val) {
        if (!val) {
          this.delete_user_arr = []
          this.$refs.noticeTableRef.clearSelection()
        }
      },
    },
  },
  data() {
    return {
      tatal: 10, //总数据
      pageCount: 1, // 页数
      pageSize: 10, // 页的大小
      // 查询信息
      search_form: {
        cid: '', // 根据小区编号查询所有人行程
        pid: '',
        bid: '',
        sid: '',
        time: [], // 选择时间 可多选
        time_period: [], // 时间段
        admin_id: '', // 根据管理员编号查询该管理员发布的行程上报
      },
      search_info: {
        cid: { label: '社区', prop: 'cid' },
        admin_id: { label: '管理员编号', prop: 'admin_id' },
        start_time: { label: '发布日期', prop: 'start_time' },
      },
      is_time_period: false, // 是否以时间段查询
      tableInfo: {
        id: { label: '编号', prop: 'id' },
        cid: { label: '通知范围', prop: 'cid' },
        admin_name: { label: '管理员', prop: 'admin_name' },
        admin_id: { label: '管理员编号', prop: 'admin_id' },
        time: { label: '发布日期', prop: 'time' },
        title: { label: '标题', prop: 'title' },
      },
      notice_table: [
        {
          id: 1366,
          cid: '宾海小区3期',
          admin_id: '10005',
          admin_name: '张三',
          time: '2020-5-6',
          title: '物业公告',
          content:
            '滨海小区3期业主们请注意，明天（2020-5-7）上午10点到下午5点期间，将进行水质检测并消毒将停水一天，请业主们今天做好储水准备。',
        },
      ],
      multiple_selection: [], // 表格选择
      notice_dialog_visible: false, // 弹窗是否可见
      add_notice_dialog: {
        cid: '',
        pid: '',
        bid: '',
        sid: '',
        title: '',
        content: '',
      },
      dialog_info: {
        title: { label: '标题', prop: 'title' },
        content: { label: '内容', prop: 'content' },
        cid: { label: '通知社区', prop: 'cid' },
      },
      delete_dialog_visible: false, //删除弹窗是否可见
      delete_user_arr: [], // 删除的用户数组
      activeNames: [],
      rules: {
        ...rules,
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
      },
    }
  },
  created() {},
  methods: {
    /* 请求 */
    /* DOM 操作 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    // 搜索
    search() {
      console.log(this.search_form)
      const data = this.search_form
      console.log(data)
    },
    // 清空搜索条件
    resetSearchFields() {
      this.search_form = {
        cid: '',
        pid: '',
        bid: '',
        sid: '',
        time: [],
        time_period: [],
        admin_id: '',
      }
    },
    // 表格选择
    selectTabel(val) {
      this.multiple_selection = val
      console.log(this.multiple_selection)
    },
    // 点击 确定 最外层弹窗
    addClick() {
      this.notice_dialog_visible = false
      console.log(this.add_notice_dialog)
      this.notice_table.unshift(this.add_notice_dialog)
      this.$message({
        message: '添加成功',
        type: 'success',
      })
    },
    // 点击 取消 最外弹窗
    cancelDialog() {
      this.notice_dialog_visible = false
    },
    // 删除
    deleteNotice(id, title) {
      this.delete_dialog_visible = true
      if (isNull(id) && typeof id !== 'object') {
        let notice = {
          id,
          title,
        }
        // 单个删除
        this.delete_user_arr.push(notice)
        console.log('删除单个')
      } else {
        this.multiple_selection.forEach((item) => {
          let notice = {
            id: item.id,
            title: item.title,
          }
          this.delete_user_arr.push(notice)
        })
        console.log('删除数组')
      }
      console.log(this.delete_user_arr)
    },
    falsedeleteNotice() {
      this.delete_dialog_visible = false
      this.$message('已取消删除')
    },
    truedeleteNotice() {
      let delete_arr = []
      this.delete_user_arr.forEach((item) => {
        delete_arr.push(item.id)
      })
      console.log(delete_arr)
      this.delete_dialog_visible = false
      this.$message({
        message: '删除成功',
        type: 'success',
      })
    },
  },
}
</script>

<style lang="less" scoped>
#page-notice {
  padding: 0 0.5rem;
  .page-table {
    .el-button {
      margin: 2px 0;
    }
  }
  .el-collapse-title-div {
    display: flex;
    justify-content: space-around;
    width: 90%;
  }
  .el-form-ul {
    min-height: 2.375rem;
    border: 1px solid #dcdfe6;
  }
  /deep/.el-collapse {
    border: none;
    background-color: rgba(255, 255, 255, 0);
  }
  /deep/.el-collapse-item__content {
    padding-bottom: 0;
  }
}
.page-botton-list {
  margin-bottom: 0.625rem;
}
.ul-address-date {
  display: flex;
  .ul-address-date-item {
    flex: 1;
    margin-right: 1.25rem;
    &:last-child {
      margin-right: 0;
    }
  }
}
/deep/.el-select {
  width: 100%;
}
.address-tag {
  margin-right: 0.625rem;
  &:last-child {
    margin-right: 0;
  }
}
.upload-list-pagination {
  margin-top: 1.8rem;
  display: flex;
  justify-content: center;
}
</style>
